package com.doudou.casebook;

import static android.content.ContentValues.TAG;

import androidx.appcompat.app.AlertDialog;
import androidx.appcompat.app.AppCompatActivity;

import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.Display;
import android.view.Gravity;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.AdapterView;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

import com.doudou.casebook.adapter.AccountAdapter;
import com.doudou.casebook.db.AccountBean;
import com.doudou.casebook.db.DBManager;
import com.doudou.casebook.utils.CalendarDialog;
import com.doudou.casebook.utils.GetTodayTime;

import java.util.ArrayList;
import java.util.List;

public class HistoryActivity extends AppCompatActivity {
    ListView historyLv;
    TextView timeTv;
    List<AccountBean> mDatas;
    AccountAdapter adapter;
    int year,month;
    int selectYearPosition=-1;
    int selectMonthPosition=-1;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_history);
        historyLv = findViewById(R.id.history_lv);
        timeTv = findViewById(R.id.history_time);
        mDatas = new ArrayList<>();
        adapter = new AccountAdapter(this, mDatas);
        historyLv.setAdapter(adapter);
        String data = GetTodayTime.year + "年" + GetTodayTime.month + "月";
        timeTv.setText(data);
        year=GetTodayTime.year;
        month=GetTodayTime.month;
        loadData(year,month);
        setLVClickListener();

    }
    /*设置ListView每一个item的长按事件*/
    private void setLVClickListener() {
        historyLv.setOnItemLongClickListener(new AdapterView.OnItemLongClickListener() {
            @Override
            public boolean onItemLongClick(AdapterView<?> parent, View view, int position, long id) {
               //获取当前长按的数据项
                AccountBean accountBean = mDatas.get(position);
                deleteItem(accountBean);
                return false;

            }
        });
    }
//删除数据项
    private void deleteItem(AccountBean accountBean) {
        int id = accountBean.getId();
        AlertDialog.Builder builder = new AlertDialog.Builder(this);
        builder.setTitle("提示信息").setMessage("您确定要删除这条记录吗?")
                .setNegativeButton("取消",null)
                .setPositiveButton("确定", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        DBManager.deleteItemFromAccountById(id);
                        mDatas.remove(accountBean);//实时刷新页面数据
                        adapter.notifyDataSetChanged();
                    }
                }).create().show();
    }

    private void loadData(int year,int month) {
        List<AccountBean> list = DBManager.getAccountListOneMonthFromAccounttb(year, month);
        mDatas.clear();
        mDatas.addAll(list);
        adapter.notifyDataSetChanged();
    }

    public void onClick(View view) {
        if (view.getId() == R.id.history_iv_back) {
            finish();

        } else if (view.getId() == R.id.history_rili) {
            CalendarDialog calendarDialog = new CalendarDialog(view.getContext(),selectYearPosition,selectMonthPosition);
            calendarDialog.show();
            calendarDialog.setDialogSize();
            calendarDialog.setOnRefreshListhener(new CalendarDialog.OnRefreshListhener() {
                @Override
                public void onRefrensh(int selPos,int position, int year, int month) {
                    timeTv.setText(year+"年"+month+"月");
                    selectYearPosition = selPos;
                    selectMonthPosition=position;
                    loadData(year,month);

                }
            });

        }
    }


}